package com.File_io_.timer;

import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author 宋鑫宇
 * @version 1.0
 */
public class Timer02 {
    public static void main(String[] args) {
        //1.创建 ScheduledExecutorService 线程池，做定时器
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(3);
        //2.开启定时任务
        pool.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName());
            }
        }, 0, 2, TimeUnit.SECONDS);

        //使用线程池做定时器，当有一个定时器异常，不会影响其他定时器的运行
        pool.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + "bbb");
                System.out.println(1 / 0);
            }
        }, 0, 2, TimeUnit.SECONDS);
        /*
            线程的六种状态
                NEW         (新建状态)
                RUNNABLE    (准备就绪状态)
                BLOCKED     (锁堵塞状态)
                WAITING     (等待状态)
                TIMED_WAITING(超时等待状态)
                TERMINATED  (终止状态)
         */


    }
}
